﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp1.bean;
using WpfApp1.chaxun;
using WpfApp1.Httpwork;
using WpfApp1.Unit;

namespace WpfApp1
{
    /// <summary>
    /// ChaXunWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChaXunWindow : Window, chaxunInterface, cxXiuGaiInterface
    {
        public static Dictionary<String, packagebean> packages = new Dictionary<String, packagebean>();
        public ChaXunWindow()
        {
            InitializeComponent();
        }

        public void initData()
        {
            listpackage.Items.Clear();
            foreach (KeyValuePair<String, packagebean> p in packages)
            {
                packagebean bean = p.Value;
                listpackage.Items.Add(bean);
            }
            listpackage.Items.Refresh();
        }

        private void Button_SouSUo(object sender, RoutedEventArgs e)
        {
            data bean = new data();
            List<String> batchs =  new List<String>();
            if (TB_batch.Text.Trim()!="")
            {
                batchs.Add(TB_batch.Text.Trim());
            }
            bean.batchs = batchs;
            bean.pallet_num = TB_pallet_num.Text.Trim();
            bean.pcode = TB_pcode.Text.Trim();
            string url = MainWindow.http + "?q=server/pda/package/batch_package";
            string content = JsonConvert.SerializeObject(bean);
            Console.WriteLine("=========" + content);
            chaxunM.HttpchaxunPost(this,url,content);
        }

        private void jsondata(string data)
        {
            packages.Clear();
            var jsonarray = (JArray)JsonConvert.DeserializeObject(data);
            foreach (JObject jo in jsonarray)
            {
                packagebean bean = new packagebean();
                bean.batch = (String)jo["batch"];
                bean.pid = (String)jo["pid"];
                bean.pcode = (String)jo["pcode"];
                bean.onum = (String)jo["onum"];
                bean.pallet_num = (String)jo["pallet_num"];
                bean.position_buffer = (String)jo["position_buffer"];
                bean.status_name = (String)jo["status_name"];
                bean.zp_ctime = (String)jo["zp_ctime"];
                bean.warehousing_time = (String)jo["warehousing_time"];
                if (bean.pallet_num == "")
                {
                    bean.chaxun_button = "手动组盘";
                }
                else
                {
                    bean.chaxun_button = "移除组盘";
                }
                packages[(String)jo["pcode"]] = bean;
            }
            initData();
        }

        private void TextBox_GuoLv(object sender, TextChangedEventArgs e)
        {
            string shuruguolv = TB_GuoLv.Text;
            listpackage.Items.Clear();
            foreach (KeyValuePair<String, packagebean> p in packages)
            {

                packagebean bean = p.Value;
                if (bean.pallet_id == "0")
                {
                    if (bean.warehousing_time != "0" || bean.deliver_time != "0")
                    {
                        continue;
                    }
                    if (bean.pcode.Contains(shuruguolv))
                    {
                        listpackage.Items.Add(bean);
                    }
                }
            }
            listpackage.Items.Refresh();
        }

        private void Button_YiChu(object sender, RoutedEventArgs e)
        {
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public void chaxunok(string data)
        {
            jsondata(data);
            Console.WriteLine("=========" + dataunit.Unicode2String(data));
        }

        public void chaxunno(string e)
        {
            Console.WriteLine("eeeeeeeeeeeeeee=" + e);
        }

        public void xiugaiok(string data)
        {
        }

        public void xiugaino(string e, bool b)
        {
        }
    }

}
